package com.ziyun.erp.web.modules.quoteAttribute.controller;

import com.ziyun.erp.common.e.quoteAttribute.AttributeSearchTypeEnum;
import com.ziyun.erp.common.e.quoteAttribute.QuoteAttributeTypeEnum;
import com.ziyun.erp.common.e.quoteAttribute.QuoteAttributeValueEnum;
import com.ziyun.erp.common.utils.JsonUtils;
import com.ziyun.erp.common.utils.UserUtils;
import com.ziyun.erp.modules.quoteAttribute.entity.QuoteAttributeEntity;
import com.ziyun.erp.modules.quoteAttribute.service.QuoteAttributeService;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;
import io.renren.common.validator.ValidatorUtils;
import io.renren.modules.sys.entity.SysUserEntity;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * 报价属性--普通属性
 *
 * @author jiangli
 * @email
 * @date 2018-09-06 08:52:31
 */
@RestController
@RequestMapping("quoteAttribute/quoteattribute")
public class QuoteAttributeController {
    @Autowired
    private QuoteAttributeService quoteAttributeService;

    /**
     * 列表--普通属性
     */
    @RequestMapping("/list")
    @RequiresPermissions("quoteAttribute:quoteattribute:list")
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = quoteAttributeService.queryPage(params);
        // 存放枚举类到userdata
        Map<String, List> mapEnum = new HashMap<>();
        mapEnum.put("quoteAttributeTypeEnum", JsonUtils.enumToList(QuoteAttributeTypeEnum.class));
        //属性类型值
        mapEnum.put("quoteAttributeValueEnum", JsonUtils.enumToList(QuoteAttributeValueEnum.class)); //AttributeValueType 接口类名
        //检索分类
        mapEnum.put("attributeSearchTypeEnum", JsonUtils.enumToList(AttributeSearchTypeEnum.class));
        return R.ok().put("page", page).put("userData", mapEnum);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("quoteAttribute:quoteattribute:info")
    public R info(@PathVariable("id") Integer id) {
        QuoteAttributeEntity quoteAttribute = quoteAttributeService.selectById(id);

        return R.ok().put("quoteAttribute", quoteAttribute);
    }

    /**
     * 新增
     */
    @RequestMapping("/save")
    @RequiresPermissions("quoteAttribute:quoteattribute:save")
    public R save(@RequestBody QuoteAttributeEntity quoteAttribute) {
        SysUserEntity curentUser = UserUtils.getCurentUser();
        quoteAttribute.setCreateUser(curentUser.getUsername());
        quoteAttribute.setAttributeDefinitionParam(quoteAttribute.getAttributeDefinitionParam().replaceAll("\"", ""));
        quoteAttributeService.save(quoteAttribute);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("quoteAttribute:quoteattribute:update")
    public R update(@RequestBody QuoteAttributeEntity quoteAttribute) {
        ValidatorUtils.validateEntity(quoteAttribute);
        quoteAttributeService.updateById(quoteAttribute);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("quoteAttribute:quoteattribute:delete")
    public R delete(@RequestBody Integer[] ids) {

        return quoteAttributeService.deleteByIds(ids);
    }

    /**
     * 列表--工艺
     */
    @RequestMapping("/listCraft")
    public R listCraft(@RequestParam Map<String, Object> params) {
        PageUtils page = quoteAttributeService.queryPageCraft(params);
        // 存放枚举类到userdata
        Map<String, List> mapEnum = new HashMap<>();
        mapEnum.put("quoteAttributeTypeEnum", JsonUtils.enumToList(QuoteAttributeTypeEnum.class));
        //属性类型值
        mapEnum.put("quoteAttributeValueEnum", JsonUtils.enumToList(QuoteAttributeValueEnum.class));
        //检索分类
        mapEnum.put("attributeSearchTypeEnum", JsonUtils.enumToList(AttributeSearchTypeEnum.class));
        return R.ok().put("page", page).put("userData", mapEnum);
    }

    /**
     * 新增--工艺
     */
    @RequestMapping("/saveCraft")
    @RequiresPermissions("quoteAttribute:quoteattribute:save")
    public R saveCraft(@RequestBody QuoteAttributeEntity quoteAttribute) {
        SysUserEntity curentUser = UserUtils.getCurentUser();
        quoteAttribute.setCreateUser(curentUser.getUsername());
        quoteAttribute.setAttributeDefinitionParam(quoteAttribute.getAttributeDefinitionParam().replaceAll("\"", ""));
        quoteAttributeService.saveCraft(quoteAttribute);

        return R.ok();
    }

    /**
     * 列表--材料
     */
    @RequestMapping("/listMaterial")
    public R listMaterial(@RequestParam Map<String, Object> params) {
        PageUtils page = quoteAttributeService.queryPageMaterial(params);
        // 存放枚举类到userdata
        Map<String, List> mapEnum = new HashMap<>();
        mapEnum.put("quoteAttributeTypeEnum", JsonUtils.enumToList(QuoteAttributeTypeEnum.class));
        //属性类型值
        mapEnum.put("quoteAttributeValueEnum", JsonUtils.enumToList(QuoteAttributeValueEnum.class));
        return R.ok().put("page", page).put("userData", mapEnum);
    }

    /**
     * 新增--材料
     */
    @RequestMapping("/saveMaterial")
    @RequiresPermissions("quoteAttribute:quoteattribute:save")
    public R saveMaterial(@RequestBody QuoteAttributeEntity quoteAttribute) {
        SysUserEntity curentUser = UserUtils.getCurentUser();
        quoteAttribute.setCreateUser(curentUser.getUsername());
        quoteAttribute.setAttributeDefinitionParam(quoteAttribute.getAttributeDefinitionParam().replaceAll("\"", ""));
        quoteAttributeService.saveMaterial(quoteAttribute);

        return R.ok();
    }
}
